home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / w3 / url-parse.el.z / url-parse.el
Encoding:
Text File  |  1998-05-21  |  5.8 KB  |  195 lines

  1. ;;; url-parse.el --- Uniform Resource Locator parser
  2. ;; Author: wmperry
  3. ;; Created: 1997/10/17 14:08:05
  4. ;; Version: 1.7
  5. ;; Keywords: comm, data, processes
  6.  
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8. ;;; Copyright (c) 1993-1996 by William M. Perry <wmperry@cs.indiana.edu>
  9. ;;; Copyright (c) 1996, 1997 Free Software Foundation, Inc.
  10. ;;;
  11. ;;; This file is not part of GNU Emacs, but the same permissions apply.
  12. ;;;
  13. ;;; GNU Emacs is free software; you can redistribute it and/or modify
  14. ;;; it under the terms of the GNU General Public License as published by
  15. ;;; the Free Software Foundation; either version 2, or (at your option)
  16. ;;; any later version.
  17. ;;;
  18. ;;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. ;;; GNU General Public License for more details.
  22. ;;;
  23. ;;; You should have received a copy of the GNU General Public License
  24. ;;; along with GNU Emacs; see the file COPYING.  If not, write to the
  25. ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  26. ;;; Boston, MA 02111-1307, USA.
  27. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  28. (defmacro url-type (urlobj)
  29.   (` (aref (, urlobj) 0)))
  30.  
  31. (defmacro url-user (urlobj)
  32.   (` (aref (, urlobj) 1)))
  33.  
  34. (defmacro url-password (urlobj)
  35.   (` (aref (, urlobj) 2)))
  36.  
  37. (defmacro url-host (urlobj)
  38.   (` (aref (, urlobj) 3)))
  39.  
  40. (defmacro url-port (urlobj)
  41.   (` (or (aref (, urlobj) 4)
  42.      (if (url-fullness (, urlobj))
  43.          (cdr-safe (assoc (url-type (, urlobj)) url-default-ports))))))
  44.  
  45. (defmacro url-filename (urlobj)
  46.   (` (aref (, urlobj) 5)))
  47.  
  48. (defmacro url-target (urlobj)
  49.   (` (aref (, urlobj) 6)))
  50.  
  51. (defmacro url-attributes (urlobj)
  52.   (` (aref (, urlobj) 7)))
  53.  
  54. (defmacro url-fullness (urlobj)
  55.   (` (aref (, urlobj) 8)))
  56.  
  57. (defmacro url-set-type (urlobj type)
  58.   (` (aset (, urlobj) 0 (, type))))
  59.  
  60. (defmacro url-set-user (urlobj user)
  61.   (` (aset (, urlobj) 1 (, user))))
  62.  
  63. (defmacro url-set-password (urlobj pass)
  64.   (` (aset (, urlobj) 2 (, pass))))
  65.  
  66. (defmacro url-set-host (urlobj host)
  67.   (` (aset (, urlobj) 3 (, host))))
  68.  
  69. (defmacro url-set-port (urlobj port)
  70.   (` (aset (, urlobj) 4 (, port))))
  71.  
  72. (defmacro url-set-filename (urlobj file)
  73.   (` (aset (, urlobj) 5 (, file))))
  74.  
  75. (defmacro url-set-target (urlobj targ)
  76.   (` (aset (, urlobj) 6 (, targ))))
  77.  
  78. (defmacro url-set-attributes (urlobj targ)
  79.   (` (aset (, urlobj) 7 (, targ))))
  80.  
  81. (defmacro url-set-full (urlobj val)
  82.   (` (aset (, urlobj) 8 (, val))))
  83.   
  84. (defun url-recreate-url (urlobj)
  85.   (concat (url-type urlobj) ":" (if (url-host urlobj) "//" "")
  86.       (if (url-user urlobj)
  87.           (concat (url-user urlobj)
  88.               (if (url-password urlobj)
  89.               (concat ":" (url-password urlobj)))
  90.               "@"))
  91.       (url-host urlobj)
  92.       (if (and (url-port urlobj)
  93.            (not (equal (url-port urlobj)
  94.                    (cdr-safe (assoc (url-type urlobj)
  95.                         url-default-ports)))))
  96.           (concat ":" (url-port urlobj)))
  97.       (or (url-filename urlobj) "/")
  98.       (if (url-target urlobj)
  99.           (concat "#" (url-target urlobj)))
  100.       (if (url-attributes urlobj)
  101.           (concat ";"
  102.               (mapconcat
  103.                (function
  104.             (lambda (x)
  105.               (if (cdr x)
  106.                   (concat (car x) "=" (cdr x))
  107.                 (car x)))) (url-attributes urlobj) ";")))))
  108.  
  109. (defun url-generic-parse-url (url)
  110.   "Return a vector of the parts of URL.
  111. Format is:
  112. [proto username password hostname portnumber file reference attributes fullp]"
  113.   (cond
  114.    ((null url)
  115.     (make-vector 9 nil))
  116.    ((or (not (string-match url-nonrelative-link url))
  117.     (= ?/ (string-to-char url)))
  118.     (let ((retval (make-vector 9 nil)))
  119.       (url-set-filename retval url)
  120.       (url-set-full retval nil)
  121.       retval))
  122.    (t
  123.     (save-excursion
  124.       (set-buffer (get-buffer-create " *urlparse*"))
  125.       (set-syntax-table url-mailserver-syntax-table)
  126.       (let ((save-pos nil)
  127.         (prot nil)
  128.         (user nil)
  129.         (pass nil)
  130.         (host nil)
  131.         (port nil)
  132.         (file nil)
  133.         (refs nil)
  134.         (attr nil)
  135.         (full nil)
  136.         (inhibit-read-only t))
  137.     (erase-buffer)
  138.     (insert url)
  139.     (goto-char (point-min))
  140.     (setq save-pos (point))
  141.     (if (not (looking-at "//"))
  142.         (progn
  143.           (skip-chars-forward "a-zA-Z+.\\-")
  144.           (downcase-region save-pos (point))
  145.           (setq prot (buffer-substring save-pos (point)))
  146.           (skip-chars-forward ":")
  147.           (setq save-pos (point))))
  148.  
  149.     ;; We are doing a fully specified URL, with hostname and all
  150.     (if (looking-at "//")
  151.         (progn
  152.           (setq full t)
  153.           (forward-char 2)
  154.           (setq save-pos (point))
  155.           (skip-chars-forward "^/")
  156.           (setq host (buffer-substring save-pos (point)))
  157.           (if (string-match "^\\([^@]+\\)@" host)
  158.           (setq user (url-match host 1)
  159.             host (substring host (match-end 0) nil)))
  160.           (if (and user (string-match "\\([^:]+\\):\\(.*\\)" user))
  161.           (setq pass (url-match user 2)
  162.             user (url-match user 1)))
  163.           (if (string-match ":\\([0-9+]+\\)" host)
  164.           (setq port (url-match host 1)
  165.             host (substring host 0 (match-beginning 0))))
  166.           (if (string-match ":$" host)
  167.           (setq host (substring host 0 (match-beginning 0))))
  168.           (setq host (downcase host)
  169.             save-pos (point))))
  170.     ;; Now check for references
  171.     (setq save-pos (point))
  172.     (skip-chars-forward "^#")
  173.     (if (eobp)
  174.         nil
  175.           (delete-region
  176.            (point)
  177.            (progn
  178.              (skip-chars-forward "#")
  179.              (setq refs (buffer-substring (point) (point-max)))
  180.              (point-max))))
  181.     (goto-char save-pos)
  182.     (skip-chars-forward "^;")
  183.     (if (not (eobp))
  184.         (setq attr (mm-parse-args (point) (point-max))
  185.           attr (nreverse attr)))
  186.     (setq file (buffer-substring save-pos (point)))
  187.     (and port (string= port (or (cdr-safe (assoc prot url-default-ports))
  188.                     ""))
  189.          (setq port nil))
  190.     (if (and host (string-match "%[0-9][0-9]" host))
  191.         (setq host (url-unhex-string host)))
  192.     (vector prot user pass host port file refs attr full)))))) 
  193.  
  194. (provide 'url-parse)
  195.